package com.taokatao.cardmee.business;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taokatao.cardmee.App;
import com.taokatao.cardmee.C;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.business.cardcrud.CardAddByCodeActivity;
import com.taokatao.cardmee.business.login.LoginActivity;
import com.taokatao.cardmee.db.CardStoreDao;
import com.taokatao.cardmee.db.model.CardStore;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class CardGuideLayout extends LinearLayout implements View.OnClickListener {

    @BindView(R.id.l_business_1) View lBusiness1;
    @BindView(R.id.i_logo_1) ImageView iLogo1;
    @BindView(R.id.t_business_1) TextView tBusiness1;

    @BindView(R.id.l_business_2) View lBusiness2;
    @BindView(R.id.i_logo_2) ImageView iLogo2;
    @BindView(R.id.t_business_2) TextView tBusiness2;

    @BindView(R.id.l_business_3) View lBusiness3;
    @BindView(R.id.i_logo_3) ImageView iLogo3;
    @BindView(R.id.t_business_3) TextView tBusiness3;

    @BindView(R.id.i_guide_close) ImageView iGuideClose;

    private List<CardStore> hotStoreList;

    public CardGuideLayout(Context context) {
        this(context, null);
    }

    public CardGuideLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_card_guide, this));
        setOnClickListener(this);

        if (isInEditMode()) return;

        boolean isOnceCardAddGuide = Utils.spReadBoolean(C.sp.ONCE_CARD_ADD_GUIDE, true);
        if (!isOnceCardAddGuide) {
            setVisibility(View.GONE);
            return;
        }
    }

    @Override
    public void setVisibility(int visibility) {
        if (View.VISIBLE == visibility) {
            boolean isOnceCardAddGuide = Utils.spReadBoolean(C.sp.ONCE_CARD_ADD_GUIDE, true);
            if (!isOnceCardAddGuide) return;
        }
        super.setVisibility(visibility);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (isInEditMode()) return;

        CardStoreDao cardStoreDao = App.i().getCommonDBSession().getCardStoreDao();
        hotStoreList = cardStoreDao.queryBuilder().where(CardStoreDao.Properties.IfHot.eq(CardStore.STATUS_HOT))
                .orderDesc(CardStoreDao.Properties.UseCount)
                .limit(3).list();
        if (hotStoreList.size() > 0) {
            lBusiness1.setVisibility(View.VISIBLE);
            Glide.with(getContext()).load(hotStoreList.get(0).getBusinessIcon())
                    .bitmapTransform(App.i().getRoundedCornersTransformation())
                    .into(iLogo1);
            tBusiness1.setText(hotStoreList.get(0).getBusinessName());
        } else {
            lBusiness1.setVisibility(View.GONE);
        }

        if (hotStoreList.size() > 1) {
            lBusiness2.setVisibility(View.VISIBLE);
            Glide.with(getContext()).load(hotStoreList.get(1).getBusinessIcon())
                    .into(iLogo2);
            tBusiness2.setText(hotStoreList.get(1).getBusinessName());
        } else {
            lBusiness2.setVisibility(View.GONE);
        }

        if (hotStoreList.size() > 2) {
            lBusiness3.setVisibility(View.VISIBLE);
            Glide.with(getContext()).load(hotStoreList.get(2).getBusinessIcon())
                    .into(iLogo3);
            tBusiness3.setText(hotStoreList.get(2).getBusinessName());
        } else {
            lBusiness3.setVisibility(View.GONE);
        }
    }

    @Override
    @OnClick({R.id.l_business_1, R.id.l_business_2, R.id.l_business_3, R.id.i_guide_close})
    public void onClick(View v) {
        if (v == lBusiness1) {
            if (hotStoreList.size() > 0) redirectToCardAddPage(hotStoreList.get(0));
        } else if (v == lBusiness2) {
            if (hotStoreList.size() > 1) redirectToCardAddPage(hotStoreList.get(1));
        } else if (v == lBusiness3) {
            if (hotStoreList.size() > 2) redirectToCardAddPage(hotStoreList.get(2));
        } else if (v == iGuideClose) {
            animate().alpha(0).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    setVisibility(View.GONE);
                    Utils.spWrite(C.sp.ONCE_CARD_ADD_GUIDE, false);
                }
            }).start();
        }
    }

    private void redirectToCardAddPage(CardStore cardStore) {
        Utils.onMobEvent(getContext(), "clickCardOfNoCard");
        if (App.i().getCurrUser() == null) {
            Intent intent = new Intent(getContext(), LoginActivity.class);
            getContext().startActivity(intent);
        } else {
            Intent intent = new Intent(getContext(), CardAddByCodeActivity.class);
            intent.putExtra("extra_card_store", cardStore);
            getContext().startActivity(intent);
        }
    }
}
